/**
 * 
 */
package fr.flogo.destinee.objects.statics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import fr.flogo.destinee.objects.IGLObject;

/**
 * @author golestif
 * 
 */
public class Cube implements IGLObject {
//	private float[][] vertices = new float[][] { new float[] {/*
//			 * <br
//			 * />
//			 */
//1, 1, -1,/* <br /> */
//-1, 1, -1,/* <br /> */
//-1, 1, 1,/* <br /> */
//1, 1, 1 /* <br /> */
//},/* <br /> */
//new float[] { // /* <br /> */
//1, -1, 1, -1, -1, 1,/* <br /> */
//-1, -1, -1,/* <br /> */
//1, -1, -1 /* <br /> */
//},/* <br /> */
//new float[] {/* <br /> */
//1, 1, 1,/* <br /> */
//-1, 1, 1,/* <br /> */
//-1, -1, 1,/* <br /> */
//1, -1, 1 /* <br /> */
//},/* <br /> */
//new float[] {/* <br /> */
//1, -1, -1,/* <br /> */
//-1, -1, -1,/* <br /> */
//-1, 1, -1,/* <br /> */
//1, 1, -1 /* <br /> */
//},/* <br /> */
//new float[] { /* <br /> */
//-1, 1, 1,/* <br /> */
//-1, 1, -1,/* <br /> */
//-1, -1, -1,/* <br /> */
//-1, -1, 1 /* <br /> */
//}, new float[] { /* <br /> */
//1, 1, -1,/* <br /> */
//1, 1, 1,/* <br /> */
//1, -1, 1,/* <br /> */
//1, -1, -1 /* <br /> */
//}, };
//
//private float[][] verticesTexture = new float[][] {/* <br /> */
//new float[] {/* <br /> */
//1, 0,/* <br /> */
//1, 1,/* <br /> */
//0, 1,/* <br /> */
//0, 0 /* <br /> */
//},/* <br /> */
//new float[] {/* <br /> */
//0, 0,/* <br /> */
//1, 0,/* <br /> */
//1, 1,/* <br /> */
//0, 1 /* <br /> */
//},/* <br /> */
//new float[] { /* <br /> */
//1, 1,/* <br /> */
//0, 1,/* <br /> */
//0, 0,/* <br /> */
//1, 0 /* <br /> */
//},/* <br /> */
//new float[] { /* <br /> */
//0, 1,/* <br /> */
//0, 0,/* <br /> */
//1, 0,/* <br /> */
//1, 1 /* <br /> */
//},/* <br /> */
//new float[] { /* <br /> */
//1, 1,/* <br /> */
//0, 1,/* <br /> */
//0, 0,/* <br /> */
//1, 0 /* <br /> */
//},/* <br /> */
//new float[] { /* <br /> */
//0, 1,/* <br /> */
//0, 0,/* <br /> */
//1, 0,/* <br /> */
//1, 1 /* <br /> */
//},/* <br /> */
//};
	
	
	private float vertices[] = { -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
			1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
			-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
			-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
			-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
			1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
			1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
			1.0f, -1.0f, 1.0f, 1.0f, -1.0f, };

	private float textureVertices[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
			0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
			1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
			1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
			0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, };
	
	private byte indices[] = {
			// Faces definition
			0, 1, 3, 0, 3, 2, 		// Face front
			4, 5, 7, 4, 7, 6, 		// Face right
			8, 9, 11, 8, 11, 10, 	// ...
			12, 13, 15, 12, 15, 14, 
			16, 17, 19, 16, 19, 18, 
			20, 21, 23, 20, 23, 22, 
									};

	private FloatBuffer textureBuffer;
	private FloatBuffer vertexBuffer;

	private int texture;

	private ByteBuffer indexBuffer = null;
	
	private float[] translation = {0.0f, 0.0f, 0.0f};

	private float[] rotation = {0.0f, 0.0f, 0.0f, 0.0f};

//	private FloatBuffer[] vertexBuffer;

//	private FloatBuffer[] textureBuffer;

	public Cube(int texture) {
		this.texture = texture;
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		byteBuffer = ByteBuffer.allocateDirect(textureVertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuffer.asFloatBuffer();
		textureBuffer.put(textureVertices);
		textureBuffer.position(0);
		
		indexBuffer  = ByteBuffer.allocateDirect(indices.length);
		indexBuffer.put(indices);
		indexBuffer.position(0);
		
		
//		vertexBuffer = new FloatBuffer[6];
//		textureBuffer = new FloatBuffer[6];
//		for (int i = 0; i < 6; i++) {
//			ByteBuffer vertexByteBuffer = ByteBuffer
//					.allocateDirect(vertices[i].length * 4);
//			vertexByteBuffer.order(ByteOrder.nativeOrder());
//			vertexBuffer[i] = vertexByteBuffer.asFloatBuffer();
//			vertexBuffer[i].put(vertices[i]);
//			vertexBuffer[i].position(0);
//
////			ByteBuffer textureByteBuffer = ByteBuffer
////					.allocateDirect(verticesTexture[i].length * 4);
////			textureByteBuffer.order(ByteOrder.nativeOrder());
////			textureBuffer[i] = textureByteBuffer.asFloatBuffer();
////			textureBuffer[i].put(verticesTexture[i]);
////			textureBuffer[i].position(0);
//			
//		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.flogo.destinee.objects.IGLObject#draw(javax.microedition.khronos.opengles
	 * .GL10)
	 */
	@Override
	public void draw(GL10 gl) {
		gl.glPushMatrix();
		gl.glTranslatef(this.translation[0], this.translation[1], this.translation[2]);
		gl.glRotatef(this.rotation[0], this.rotation[1], this.rotation[2], this.rotation[3]);
		
		//Bind the texture according to the set texture filter
				gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);

				//Enable the vertex, texture and normal state
				gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
				gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

				//Set the face rotation
//				gl.glFrontFace(GL10.GL_CCW);
				
				
				
				//Point to our buffers
				gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
				gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
				
				//Draw the vertices as triangles, based on the Index Buffer information
				gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
				
//				int i = 0;
//				while (i < 6) {
//					gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer[i]);
////					gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, cubeTextureBfr[i]);
//					gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
//					i++;
//				}
				
				//Disable the client state before leaving
				gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
				gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

				// TODO speed et rotations et translationss
				
				gl.glPopMatrix();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.flogo.destinee.objects.IGLObject#translate(float, float, float)
	 */
	@Override
	public void translate(float x, float y, float z) {
		this.translation[0] = x;
		this.translation[1] = y;
		this.translation[2] = z;

	}
	
	public void rotation(float angle, float x, float y, float z) {
		this.rotation[0] = angle;
		this.rotation[1] = x;
		this.rotation[2] = y;
		this.rotation[3] = z;
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.flogo.destinee.objects.IGLObject#isFranchisable()
	 */
	@Override
	public boolean isFranchisable() {
		// TODO Auto-generated method stub
		return false;
	}

}
